export function init() {
    // 加载config
    let config = window.$config;
    let t = utools.db.get('config');
    if (t) {
        config = t.data;
        window.$config = config;
        console.log('config init', config);
    }
    if (config) {
        console.log('config', config);
        const wrap = config.wrap;
        $('#objKey').val(wrap);
        const lang = config.lang;
        $('#configEl .lang').html('');
        lang.map((item) => {
            const lineEl = $('<div class="line"></div>');
            const rowEL = $(`<div class="row" id="${item.code}"></div>`);
            const label = $('<div class="label"></div>');
            label.text(item.name);
            const code = $('<div class="code"></div>');
            code.text(item.code);
            const file = $('<div class="file"></div>');
            file.text(item.file);
            const selectFileD = $(
                `<input accept=".json,.js"  id="file-${item.code}" type="file" />`
            );
            const selectFile = $(
                `<button  id="file-${item.code}" >修改路径</button>`
            );
            selectFile.on('click', (e) => {
                const fileArr = utools.showOpenDialog({
                    filters: [{ name: '', extensions: ['json', 'js'] }],
                    properties: ['openFile'],
                });
                console.log('file', fileArr);
                // console.log('fileChanged', e);
                // console.log('selectFile.files', selectFile.get(0).files);
                const fpath = fileArr[0];
                // 缓存
                utools.db.remove('config');
                window.$config.lang.map((l) => {
                    if (l.code === item.code) {
                        console.log('l', l);
                        l.file = fpath;
                        file.text(item.file);
                    }
                });
                utools.db.remove('config');
                utools.db.put({
                    _id: 'config',
                    data: window.$config,
                });
                console.log('get', utools.db.get('config'));
            });
            /* selectFile.on('change', (e) => {
            console.log('fileChanged', e);
            console.log('selectFile.files', selectFile.get(0).files);
            const f = selectFile.get(0).files[0];
            // 缓存
            utools.db.remove('config');
            window.$config.lang.map((l) => {
                if (l.code === item.code) {
                    console.log('l', l);
                    l.file = f.path;
                    file.text(item.file);
                }
            });
            utools.db.remove('config');
            utools.db.put({
                _id: 'config',
                data: window.$config,
            });
            console.log('get', utools.db.get('config'));
        }); */
            const data = $('<div class="data"></div>');
            const tips = $('<div class="tips"></div>');
            const inputEl = $(`<input type="text" id="rewrite-${item.code}" />`);
            const btn = $(`<button class="rewrite">重新写入</button>`);
            data.text('待翻译');
            inputEl.val('待翻译');
            lineEl.append(label, code, file, selectFile);
            rowEL.append(lineEl, data, inputEl, btn, tips);
            $('#configEl .lang').append(rowEL);
            btn.on('click', async function (e) {
                console.log('click', this, e);
                const val = inputEl.val();
                let key = $('#objKey').val();
                console.log('value', val);
                tips.removeClass('success');
                tips.removeClass('error');
                tips.text();
                try {
                    await window.$readTranslateOne(item.code, key, val, true);
                    tips.addClass('success');
                    tips.text('重写成功');
                } catch (e) {
                    console.error(e)
                    tips.addClass('error');
                    tips.text('重写失败');
                }
            });
        });
    } else {
        console.log('no config');
        $('#configEl').html('配置文件不存在');
    }
}
